﻿
Imports std
Imports osi.fs.property

Partial Public Class node
    Partial Public Class locked_properties
        Private Delegate Function workon_property_delegate(Of T)(ByVal p As iproperty(Of T)) As eventComb
        Private Delegate Function get_property_delegate(Of T)(ByVal o As pointer(Of iproperty(Of T))) As eventComb

        Private Function workon_property(Of T) _
                                        (ByVal key As String,
                                         ByVal [get] As get_property_delegate(Of T),
                                         ByVal d As workon_property_delegate(Of T)) As eventComb
            assert(Not d Is Nothing)
            Dim p As pointer(Of iproperty(Of T)) = Nothing
            Dim ec As eventComb = Nothing
            Return New eventComb(Function() As Boolean
                                     p = New pointer(Of iproperty(Of T))()
                                     ec = [get](p)
                                     Return waitfor(ec) AndAlso
                                            goto_next()
                                 End Function,
                                 Function() As Boolean
                                     If ec.end_result() Then
                                         assert(Not +p Is Nothing)
                                         Return waitfor(d(+p)) AndAlso
                                                goto_end()
                                     Else
                                         Return False
                                     End If
                                 End Function)
        End Function

        Private Function create_workon_property(Of T, PT As iproperty(Of T)) _
                                               (ByVal key As String,
                                                ByVal d As workon_property_delegate(Of T)) As eventComb
            Return workon_property(Of T)(key, Function(x) create_get(Of T, PT)(key, x), d)
        End Function

        Private Function get_workon_property(Of T)(ByVal key As String,
                                                   ByVal d As workon_property_delegate(Of T)) As eventComb
            Return workon_property(Of T)(key, Function(x) [get](Of T)(key, x), d)
        End Function

        Public Function get_data(Of T) _
                                (ByVal key As String, ByVal output As pointer(Of T)) As eventComb
            Return get_workon_property(Of T)(key, Function(p) p.get_data(output))
        End Function

        Public Function set_data(Of T) _
                                (ByVal key As String, ByVal value As T) As eventComb
            Return get_workon_property(Of T)(key, Function(p) p.set_data(value))
        End Function

        Public Function get_set_data(Of T) _
                                    (ByVal key As String, ByVal d As _do(Of T, Boolean)) As eventComb
            Return get_workon_property(Of T)(key, Function(p) p.get_set_data(d))
        End Function

        Public Function get_set_data(Of T) _
                                    (ByVal key As String, ByVal d As get_set_data_d(Of T)) As eventComb
            Return get_workon_property(Of T)(key, Function(p) p.get_set_data(d))
        End Function

        Public Function create_get_data(Of T, PT As iproperty(Of T)) _
                                       (ByVal key As String, ByVal output As pointer(Of T)) As eventComb
            Return create_workon_property(Of T, PT)(key, Function(p) p.get_data(output))
        End Function

        Public Function create_set_data(Of T, PT As iproperty(Of T)) _
                                       (ByVal key As String, ByVal value As T) As eventComb
            Return create_workon_property(Of T, PT)(key, Function(p) p.set_data(value))
        End Function

        Public Function create_get_set_data(Of T, PT As iproperty(Of T)) _
                                           (ByVal key As String, ByVal d As _do(Of T, Boolean)) As eventComb
            Return create_workon_property(Of T, PT)(key, Function(p) p.get_set_data(d))
        End Function

        Public Function create_get_set_data(Of T, PT As iproperty(Of T)) _
                                           (ByVal key As String, ByVal d As get_set_data_d(Of T)) As eventComb
            Return create_workon_property(Of T, PT)(key, Function(p) p.get_set_data(d))
        End Function
    End Class
End Class
